package com.yingsheng.bi.hks.repository.dao.service;

import com.yingsheng.bi.hks.dto.job.JobsQryReq;
import com.yingsheng.bi.hks.repository.dao.mapper.JobInfoMapper;
import com.yingsheng.bi.hks.repository.dao.po.JobInfo;
import com.yingsheng.bi.hks.repository.dao.po.JobInfoExample;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * job_info表的数据库操作服务
 * 要求说明：
 * 1.一般情况下,要求参数不超过5个时方法入参逐个声明
 * 2.一般情况下,要求条件不超过3个时要在方法名上体现
 * 3.最好对方法入参做一定校验,尤其是对条件参数至少要做非空校验
 *
 * @author 83686
 */
@Repository
public class JobInfoDaoService {

    @Autowired
    private JobInfoMapper jobInfoMapper;

    @Autowired
    private SequenceDaoService sequenceDaoService;

    public boolean insert(JobInfo jobInfo) {
        jobInfo.setJobBh(String.valueOf(sequenceDaoService.getNextVal("job_bh")));
        int result = jobInfoMapper.insert(jobInfo);
        return result == 1;
    }

    public boolean updateById(JobInfo jobInfo) {
        jobInfo.setUpdateTime(new Date());
        int result = jobInfoMapper.updateByPrimaryKey(jobInfo);
        return result == 1;
    }

    public List<JobInfo> query(JobsQryReq req){
        JobInfoExample example = new JobInfoExample();
        JobInfoExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isNullOrEmpty(req.getUserId())){
            criteria.andUserIdEqualTo(req.getUserId());
        }
        if (!StringUtil.isNullOrEmpty(req.getName())){
            criteria.andNameEqualTo(req.getName());
        }
        if (!StringUtil.isNullOrEmpty(req.getPhone())){
            criteria.andPhoneEqualTo(req.getPhone());
        }
        if (!StringUtil.isNullOrEmpty(req.getState())){
            criteria.andStateEqualTo(req.getState());
        }
        if (!StringUtil.isNullOrEmpty(req.getJobBh())){
            criteria.andJobBhEqualTo(req.getJobBh());
        }
        example.setOrderByClause("create_time desc");
        return jobInfoMapper.selectByExample(example);
    }

    public JobInfo queryById(String jobId){
        return jobInfoMapper.selectByPrimaryKey(jobId);
    }

    public List<JobInfo> queryForApply(String userId,String jobId){
        JobInfoExample example = new JobInfoExample();
        JobInfoExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isNullOrEmpty(jobId)){
            criteria.andIdEqualTo(jobId);
        }
        if (!StringUtil.isNullOrEmpty(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        return jobInfoMapper.selectByExample(example);
    }

    public boolean del(String id){
        return jobInfoMapper.deleteByPrimaryKey(id) == 1;
    }
}
